<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Gallery extends Controller {
		
	public function action_index(){			
		$this->response->body(Helper_Widjets::galleryLanding());								
	}
	
	public function action_upload(){
		$this->tryUpload();			
	}
	
	public function action_get(){
		$id = intval(Request::current()->param('id'));
		$this->ajaxResponse('image',Model_Gallery::getImageInfo($id));
		$this->ajaxOutput();
	}
	
	public function action_items(){
		$page = intval(Request::current()->param('id'));	
		$rowsPerPage = 12;			
		$data = Model_Gallery::itemsList($page*$rowsPerPage, $rowsPerPage);	
		$this->ajaxResponse('html', Helper_Widjets::photosPreview($data));			
		$this->ajaxResponse('logPaging', array('onPage'=>$rowsPerPage, 'ns'=>Model_Gallery::getCountItems(), 'activePage'=>$page));	
		$this->ajaxOutput();
	}
	
	
	
	
	
	public function tryUpload(){
		$name = $this->request->post('name');		
	    $file = Arr::get($_FILES, 'image', '');		
		
		$errors = $this->checkErrors($name,$file);
		if($errors){				
			$this->response->body(Helper_Widjets::galleryLanding($errors));			
		}
		else {
			if(getimagesize($file['tmp_name'])){			
				$id = Model_Gallery::insertImageInfo($name);				
				include_once('application/classes/helper/image.class.php');
				$thumb = new easyphpthumbnail;
				//$thumb->Thumbsize = 100;		
				$thumb -> Thumbheight = 200;
				$thumb -> Thumbwidth = 150;
				$thumb -> Percentage = true;
				$thumb -> Quality = 100;
				$thumb -> Thumblocation = 'pub/gallery/';
				$thumb -> Thumbprefix = '';
				$thumb -> Thumbsaveas = 'jpg';				
				$thumb -> Thumbfilename = 'av_'.$id[0].'.jpg';
				$thumb -> Createthumb($file['tmp_name'], 'file');
				
				
				////////// save 600px size
				
				$thumb = new easyphpthumbnail;
				//$thumb->Thumbsize = 600;		
				$thumb -> Thumbheight = 600;
				$thumb -> Thumbwidth = 450;
				$thumb -> Percentage = true;
				$thumb -> Quality = 100;
				$thumb -> Thumblocation = 'pub/gallery/';
				$thumb -> Thumbprefix = '';
				$thumb -> Thumbsaveas = 'jpg';				
				$thumb -> Thumbfilename = 'full_'.$id[0].'.jpg';
				$thumb -> Createthumb($file['tmp_name'], 'file');
				$this->request->redirect('/gallery');				
			}
		
		}
	
	
	}
	
	
	
	public function checkErrors($name,$file){
		$errors = false;

		
		if( Helper_Checker::imgName($name) ){
			$errors[] = __('g.upload.error.name');
		}
		
		if( Helper_Checker::fileEmpty($file) ){
			$errors[] = __('g.upload.error.empty');
		}
		
		if( Helper_Checker::fileImage($file) ){
			$errors[] = __('g.upload.error.ext');
		}
		
		if( Helper_Checker::fileSize($file) ){
			$errors[] = __('g.upload.error.size');
		}
		return $errors;			
	}
	
	
	public function action_delete (){
		($this->request->param('id')>0) ?	$id = $this->request->param('id') : $id = 0;
		
		if($id){
			$this->deleteFiles($id);
			Model_Gallery::deleteItem($id);
						
		}		
		$this->request->redirect('/gallery');
	}
	
	public function deleteFiles($id){
		if(is_file('pub/gallery/av_'.$id.'.jpg')){
			unlink('pub/gallery/av_'.$id.'.jpg');
		}	

		if(is_file('pub/gallery/full_'.$id.'.jpg')){
			unlink('pub/gallery/full_'.$id.'.jpg');
		}	
	}
	
	
	

	
	
	
	
	


	
	
	
	
	
	
	
	function ajaxResponse($key,$data){
		$this->ajaxSuccess['success'][$key] = $data;		
	}
	
	function ajaxOutput(){
		$this->response->body(json_encode($this->ajaxSuccess));		
	}
	

	
	

}
